/*
 * Copyright (c) 2009 John Pritchard, WTKX Project Group
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package wtkx.ui;

import wtkx.Component;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.font.FontRenderContext;
import java.awt.font.GlyphMetrics;
import java.awt.font.GlyphVector;
import java.awt.geom.Path2D;
import java.awt.geom.Rectangle2D;
import java.nio.charset.Charset;
import java.util.StringTokenizer;

/**
 * Calculates coordinate deltas for displaying a view within a window.
 */
public class Scrolling
    extends Object
{
    protected final Component component;

    protected Rectangle2D.Float window, view;

    protected float end, line;
    /**
     * A positive magnitude is subtracted from text-view virtual pixel
     * position, so that 'end' is positive.
     */
    public float dy;


    public Scrolling(Component component){
        super();
        if (null != component){
            this.component = component;
        }
        else
            throw new IllegalArgumentException();
    }

    public void update(Rectangle2D.Float window, Rectangle2D.Float view){
        this.window = window;
        this.view = view;
        this.end = (view.height-window.height);
        if (0f > end){
            this.dy = 0f;
            this.end = 0f;
        }
    }
    public void see(Shape shape, float line){
        this.line = line;
        if (null != shape){
            Rectangle2D bounds = shape.getBounds2D();
            float ay = (float)(bounds.getY()-this.dy);
            float ah = (float)bounds.getHeight();
            float cy = this.window.y;
            float ch = this.window.height;

            if (ah > ch)
                /*
                 * Selection greater than window
                 */
                return;
            else {
                if (cy > ay)
                    this.home(cy-ay+line);
                else {
                    float aa = (ay+ah+line);
                    float cc = (cy+ch);
                    if (aa > cc){
                        this.end(aa-cc);
                    }
                }
            }
        }
    }
    public void home(){
        this.dy = 0;
    }
    public void end(){
        this.dy = this.end;
    }
    public void end(float dy){
        this.dy += dy;
        if (this.end > this.dy)
            this.dy = this.end;
    }
    public void home(float dy){
        this.dy -= dy;
        if (0f > this.dy)
            this.dy = 0f;
        else if (this.dy < this.line)
            this.dy = 0f;
    }
}
